package com.tianqu.tms.www.car.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.MarketCarGateInfo;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;
import com.tianqu.tms.www.car.viewhelper.CarTransferViewHelper;

/**
 * @author zhangxueliang
 * 车辆搜素中过户信息查看controller
 */
public class CarSearchTransferDetailController extends TmsController{
	private Logger logger = Logger.getLogger(CarSearchTransferDetailController.class);
	public final static String CARTRANSFERVIEWHELPER = "CarTransferViewHelper";
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String basicCarId = (String)uc.getAttribute("serial");
			CarTransferViewHelper viewHelper = new CarTransferViewHelper();
			Car car = Car.getInstance();
			car = car.findCarBySerial(basicCarId);
			viewHelper = entityToViewHelper(viewHelper,car);
			// huangwei st2
			CarCreationInputViewHelper view = new CarCreationInputViewHelper();
			Object obj = uc.getAttribute(TmsConstants.VIEWHELP);
			if(obj!=null && obj instanceof CarCreationInputViewHelper){
				view = (CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
				viewHelper.setmStatus(view.getStatus());
			}
			//
			uc.setAttribute(CARTRANSFERVIEWHELPER, viewHelper);
			controller.commit();
			
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}
	
	/**
	 * entity -> viewhelper
	 * @param viewhelper
	 * @param entity
	 * @return
	 * @throws TmsDaoException 
	 * @throws ParseException 
	 */
	private CarTransferViewHelper entityToViewHelper(CarTransferViewHelper viewhelper,Car entity) throws TmsDaoException, ParseException{
		Codes code = Codes.getInstance();
		viewhelper.setSerial(entity.getSerial());//车辆编号
		viewhelper.setBasicCarId(entity.getSerial());//基本信息关联ID		
		viewhelper.setRegisterStatusInfo(code.getItemValueByItemIdAndItemCode(Codes.REGISTERSTATUS_TYPE, entity.getRegisterStatus()));//库存状态
		viewhelper.setBarCodeNumInfo(code.getItemValueByItemIdAndItemCode(Codes.BARCODENUM_TYPE, entity.getBarCodeNum()));//挂起状态
		viewhelper.setLicensePlate(entity.getLicensePlate());//车牌号码
		viewhelper.setVin(entity.getVin());//车架号
		viewhelper.setNewLicensePlate(entity.getNewLicensePlate());//旧车牌号码
		viewhelper.setChgLicensePlateTime(entity.getChgLicensePlateTimes());//换牌时间
		viewhelper.setRegDate(entity.getInsertTimeS());//登记时间
		viewhelper.setCarColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR,entity.getColor()));//车身颜色
		viewhelper.setStorageTime(entity.getStorageTimes());//入库时间
		viewhelper.setEnterTime(entity.getEnterTimes());//出库时间	
		//车辆进出记录（最近20次）
		int i=1;
		List<MarketCarGateInfo> r = new ArrayList<MarketCarGateInfo>();
		//出入库数据
		MarketCarGateInfo marketCarGateInfo = MarketCarGateInfo.getInstance();
		for(MarketCarGateInfo info : marketCarGateInfo.findAllGateInfoBySerial(entity.getSerial())){
		   if(i>20) break;
		   if(i%2 == 0) info.setCssFlag(2); 
		   else info.setCssFlag(1);
		   info.setNum(i);
		   i++;
		   r.add(info);
		}
		viewhelper.setGateInfoList(r);
		return viewhelper;
	} 

}
